package com.lfs.market.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lfs.market.common.Result;
import com.lfs.market.model.R;
import com.lfs.market.model.dto.OrderDto;
import com.lfs.market.model.entity.Orders;
import com.lfs.market.model.vo.PaymentOrderVo;
import com.lfs.market.service.OrdersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单表前端操作接口
 **/
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Resource
    private OrdersService ordersService;

    /**
     * 创建订单并返回订单号
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody OrderDto ordersDto) {
        String orderNo = ordersService.createOrder(ordersDto);
        return R.ok(orderNo);
    }

    /**
     * 根据单号查询信息
     */
    @GetMapping("/info/{paymentOrderId}")
    public R<PaymentOrderVo> selectByProductNo(@PathVariable("paymentOrderId") String paymentOrderId) {
        PaymentOrderVo paymentOrderVo = ordersService.selectOrderNo(paymentOrderId);
        return R.ok(paymentOrderVo);
    }

//    /**
//     * 查询订单详情
//     * @param productOrderId
//     * @return
//     */
//    @GetMapping("/detail/{productOrderId}")
//    public R<OrderDetailVo> selectOrderDetail(@PathVariable("productOrderId") String productOrderId) {
//        OrderDetailVo paymentOrderVo = ordersService.selectOrderDetail(productOrderId);
//        return R.ok(paymentOrderVo);
//    }
    /**
     * 完成支付
     */
    @PutMapping("/pay/{orderNo}")
    public R finishPay(@PathVariable("orderNo") String orderNo) {
        ordersService.finishPay(orderNo);
        return R.ok();
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        ordersService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        ordersService.removeBatchByIds(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Orders orders) {
        ordersService.updateById(orders);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Orders orders = ordersService.getById(id);
        return Result.success(orders);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Orders> list = ordersService.list();
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "12") Integer pageSize) {
        Page<Orders> page = new Page<>(pageNum, pageSize);
        return Result.success(ordersService.page(page));
    }

}